package com.ysyc.driver_ee.activity;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.base.BaseActivity;
import com.ysyc.driver_ee.base.BaseFragment;
import com.ysyc.driver_ee.fragment.CapacityFragment;
import com.ysyc.driver_ee.fragment.MineFragment;
import com.ysyc.driver_ee.fragment.OrderFormFragment;
import com.ysyc.driver_ee.fragment.StatisticsFragment;
import com.ysyc.driver_ee.impl.ViewPagerChangeListener;
import com.ysyc.driver_ee.model.DriverInfo;
import com.ysyc.driver_ee.tools.PermissionUtils;
import com.ysyc.driver_ee.widget.CustomViewPager;
import com.ysyc.driver_ee.widget.SureDialog;

import butterknife.BindView;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/10/29 17:01<br>
 * 版本： v1.0<br>
 */
public class MainActivity extends BaseActivity {
    private final String[] TAB_TITLES = new String[]{"运力", "订单", "统计", "我的"};
    //Tab 数目
    private final int COUNT = TAB_TITLES.length;
    //Fragment 数组
    private final BaseFragment[] TAB_FRAGMENTS = new BaseFragment[COUNT];
    private final int[] TAB_IMGS = new int[]{R.drawable.capacity_tab_selector,R.drawable.order_form_tab_selector,R.drawable.statistics_tab_selector,R.drawable.mine_tab_seletor};
    @BindView(R.id.main_vp)
    CustomViewPager mainVp;
    @BindView(R.id.main_tl)
    TabLayout mainTl;
    private SureDialog sureDialogs;
    public DriverInfo driverInfo;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    private CapacityFragment capacityFragment;
    private OrderFormFragment orderFormFragment;
    private StatisticsFragment statisticsFragment;
    private MineFragment mineFragment;
    private MyViewPagerAdapter mAdapter;

    @Override
    protected void restoreSavedInstanceState(Bundle savedInstanceState) {
        //获取到保存的fragment
        if (savedInstanceState != null) {
            FragmentManager supportFragmentManager = getSupportFragmentManager();
            String tag = "android:switcher:" + R.id.main_vp + ":";
            capacityFragment = (CapacityFragment) supportFragmentManager.findFragmentByTag(tag + 0);
            orderFormFragment = (OrderFormFragment) supportFragmentManager.findFragmentByTag(tag + 1);
            statisticsFragment = (StatisticsFragment) supportFragmentManager.findFragmentByTag(tag + 2);
            mineFragment = (MineFragment) supportFragmentManager.findFragmentByTag(tag + 3);
        }
    }

    @Override
    protected void initView() {
//        认证状态 0为未认证 1为认证中 2为认证成功
        driverInfo = getIntent().getParcelableExtra(VerificationCodeActivity.DRIVER_INFO);

        if (0==driverInfo.authStatus){
            if (null==sureDialogs) {
                sureDialogs = new SureDialog(this);
            }
            sureDialogs.setTitleText("暂未认证").setMessage("暂未认证，点击前往认证").setPositiveListener(new SureDialog.onPositiveClickListener() {
                @Override
                public void onSureClick(SureDialog dialog) {
                    toActivityClearTask(DriverCertificationActivity.class);
                }
            });
            sureDialogs.show();
        }else if (1==driverInfo.authStatus){
            SureDialog sureDialog = new SureDialog(this);
            sureDialog.setTitleText("正在认证中").setMessage("信息正在认证中，请稍后").setPositiveListener(new SureDialog.onPositiveClickListener() {
                @Override
                public void onSureClick(SureDialog dialog) {
                    finish();
                }
            });
            sureDialog.show();
        }else if (3==driverInfo.authStatus){
            SureDialog sureDialog = new SureDialog(this);
            sureDialog.setTitleText("认证未通过").setMessage("认证未通过，请重新认证").setPositiveListener(new SureDialog.onPositiveClickListener() {
                @Override
                public void onSureClick(SureDialog dialog) {
                    toActivityClearTask(DriverCertificationActivity.class);
                }
            });
            sureDialog.show();
        }

        initNetworkCallback();

        if (capacityFragment == null) {
            capacityFragment = capacityFragment.newInstance(CapacityFragment.class, null);
        }
        if (orderFormFragment == null) {
            orderFormFragment = orderFormFragment.newInstance(OrderFormFragment.class, null);
        }
        if (statisticsFragment == null) {
            statisticsFragment = statisticsFragment.newInstance(StatisticsFragment.class, null);
        }
        if (mineFragment == null) {
            mineFragment = MineFragment.newInstance(MineFragment.class, null);
        }
        TAB_FRAGMENTS[0] = capacityFragment;
        TAB_FRAGMENTS[1] = orderFormFragment;
        TAB_FRAGMENTS[2] = statisticsFragment;
        TAB_FRAGMENTS[3] = mineFragment;
        //设置viewpager不能滑动
        mainVp.setScanScroll(false);
        mainVp.setOffscreenPageLimit(3);
        //设置adapter
        mAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
        mainVp.setAdapter(mAdapter);
        //设置滑动监听
        mainVp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mainTl));
        mainTl.addOnTabSelectedListener(new ViewPagerChangeListener(mainVp, TAB_TITLES));
        //设置自定义的tab
        setTabs();
    }

    private void initNetworkCallback() {


    }

    /**
     * @description: ViewPager 适配器
     */
    private class MyViewPagerAdapter extends FragmentPagerAdapter {

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public BaseFragment getItem(int position) {//指定Position所对应的页面的Fragment内容
//            BaseFragment fragment = FragmentFactory.createFragment(position);
            return TAB_FRAGMENTS[position];
        }

        @Override
        public int getCount() {//决定ViewPager页数的总和
            return COUNT;
        }
    }

    /**
     * 设置自定义的tabview
     */
    private void setTabs() {

        mainTl.setTabRippleColor(ColorStateList.valueOf(this.getResources().getColor(R.color.transparent)));

        for (int i = 0; i < COUNT; i++) {
            TabLayout.Tab tab = mainTl.newTab();
            View view = LayoutInflater.from(this).inflate(R.layout.tabs_main_item, mainTl,false);
            tab.setCustomView(view);
            view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            ImageView tabIv = view.findViewById(R.id.tab_iv);
            TextView tabTv = view.findViewById(R.id.tab_tv);
            tabTv.setText(TAB_TITLES[i]);
            tabIv.setImageDrawable(getResources().getDrawable(TAB_IMGS[i]));
            mainTl.addTab(tab);
            if (i == 0) {
                tab.select();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionResult(this, requestCode, permissions, grantResults, capacityFragment.fragment.adapter.mPermissionGrant);
    }





}
